package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public StrategyDetail get(Long id) {
            StrategyDetail detail = strategyDetailMapper.selectByPrimaryKey(id);
            StrategyContent content = contentMapper.selectByPrimaryKey(id);
            detail.setStrategyContent(content);
            return detail;
    }

    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Override
    public void saveOrUpdate(StrategyDetail entity,String tags) {

        //生成摘要,模拟ai
        entity.setSummary("智能生成.................");
        //填充攻略名称和目的地
        StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(entity.getCatalog().getId());
        entity.setCatalog(catalog);
        Destination dest = new Destination();
        dest.setId(catalog.getDestId());
        entity.setDest(dest);
        entity.setStrategyName(catalog.getStrategyName());


        StrategyContent content = entity.getStrategyContent();
        //1.处理文章内容
        if (entity.getId() == null){//新增
            //设置创建时间
            entity.setCreateTime(new Date());
            strategyDetailMapper.insert(entity);
            content.setId(entity.getId());
            contentMapper.insert(content);

            //让redis缓存改文章
            StrategyStats vo = new StrategyStats();
            vo.setDestId(entity.getId());
            strategyStatsCacheService.setStrategyStats(vo);

        }else{//更新
            strategyDetailMapper.updateByPrimaryKey(entity);
            content.setId(entity.getId());
            contentMapper.updateByPrimaryKey(content);
        }

        //处理标签
        //2.删除旧标签
        strategyDetailMapper.deleteRelation(entity.getId());
        //3.保存新标签
        if (tags != null){
            String[] ts = tags.split(",");
            for (String name : ts) {
                //查询标签是否存在
               StrategyTag tag = strategyTagMapper.selectByName(name);
               //如果不存在就新增
               if (tag == null){
                   tag = new StrategyTag();
                   tag.setName(name);
                   strategyTagMapper.insert(tag);
               }
               //保存关系
                strategyDetailMapper.insertRelation(entity.getId(),tag.getId());
            }
        }


    }

    @Override
    public String getTag(Long detailId) {
        String tag = strategyDetailMapper.selectTag(detailId);
        return tag;
    }

    @Override
    public List<StrategyDetail> selectByDestId(Long destId) {
        return strategyDetailMapper.selectByDestId(destId);
    }

    @Override
    public List<StrategyDetail> listVeiwnumTop3(Long destId) {
        return strategyDetailMapper.listVeiwnumTop3(destId);
    }

    @Override
    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.listAll();
    }


    @Override
    public void updateState(StrategyDetail detail) {
        strategyDetailMapper.updateState(detail);
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail selectByThemeIdOne(Long themeId) {
        return strategyDetailMapper.selectByThemeIdOne(themeId);
    }

    @Override
    public List<StrategyDetail> selectByThemeId(Long themeId) {
        return strategyDetailMapper.selectByThemeId(themeId);
    }

    @Override
    public List<StrategyDetail> selectByDestIds(Long[] refIds) {
        return strategyDetailMapper.selectByDestIds(refIds);
    }

    @Override
    public List<StrategyDetail> selectByCondition(Long themeId, Long[] refIds) {
        return strategyDetailMapper.selectByCondition(themeId,refIds);
    }

    @Override
    public StrategyDetail selectById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }
}
